package com.ruoyi.system.service.vantservice.customerervice.impl;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.system.domain.customer.CustomerLogin;
import com.ruoyi.system.domain.customer.PasswordTeamCode;
import com.ruoyi.system.mapper.vant.customer.ApiPasswordTeamCodeMapper;
import com.ruoyi.system.service.vantservice.customerervice.PasswordTeamCodeService;
import com.ruoyi.system.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
public class PasswordTeamCodeServiceImpl implements PasswordTeamCodeService {

    @Autowired
    private ApiPasswordTeamCodeMapper apiPasswordTeamCodeMapper;
    @Autowired
    private TokenUtils tokenUtils;
    @Autowired
    private HttpServletRequest request;



    @Override
    public PasswordTeamCode selectPasswordTeamCodeById(Long passwordId) {
        return apiPasswordTeamCodeMapper.selectPasswordTeamCodeById(passwordId);
    }

    @Override
    public List<PasswordTeamCode> selectPasswordTeamCodeList(PasswordTeamCode passwordTeamCode) {
        return apiPasswordTeamCodeMapper.selectPasswordTeamCodeList(passwordTeamCode);
    }

    @Override
    public int insertPasswordTeamCode(PasswordTeamCode passwordTeamCode) {
        CustomerLogin customerLogin = tokenUtils.getLoginCustomer(request);
        passwordTeamCode.setCustomerId(customerLogin.getCustomerId());

        StringBuilder stringBuilder = new StringBuilder();
        Random random = new Random();
        for (int i=0;i<6;i++){
            stringBuilder.append(random.nextInt(10));
        }
        String num =stringBuilder.toString();
        System.err.println("**********88888888888888"+num);
        passwordTeamCode.setPasswordcode(num);
        passwordTeamCode.setModifiedTime(new Date());
        return apiPasswordTeamCodeMapper.insertPasswordTeamCode(passwordTeamCode);
    }

    @Override
    public String checkUserNameUnique(PasswordTeamCode passwordTeamCode) {
        CustomerLogin customerLogin = tokenUtils.getLoginCustomer(request);
        passwordTeamCode.setCustomerId(customerLogin.getCustomerId());

        int count = apiPasswordTeamCodeMapper.checkUserNameUnique(passwordTeamCode);
        System.err.println("******************ccount***************"+count);
        if(count > 0){
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public int updatePasswordTeamCode(PasswordTeamCode passwordTeamCode) {
        return apiPasswordTeamCodeMapper.updatePasswordTeamCode(passwordTeamCode);
    }

    @Override
    public int deletePasswordTeamCodeByIds(Long[] passwordIds) {
        return apiPasswordTeamCodeMapper.deletePasswordTeamCodeByIds(passwordIds);
    }

    @Override
    public int deletePasswordTeamCodeById(Long passwordId) {
        return apiPasswordTeamCodeMapper.deletePasswordTeamCodeById(passwordId);
    }
}
